package com.liner.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liner.entity.OrderMaster;
import com.liner.form.BuyerOrderForm;
import com.liner.service.OrderMasterService;
import com.liner.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单详情表 前端控制器
 * </p>
 *
 * @author liner
 * @since 2022-12-03
 */
@RestController
@RequestMapping("/buyer/order")
@Api(tags = "买家端订单接口", description = "买家端 商品订单接口")
public class BuyerOrderController {

    @Autowired
    private OrderMasterService orderMasterService;


    @ApiOperation("创建订单")
    @PostMapping("/create")
    public Result create(@RequestBody BuyerOrderForm buyerOrderForm) {

        String orderId = orderMasterService.create(buyerOrderForm);
        HashMap<String, String> map = new HashMap<>();
        map.put("orderId", orderId);
        return Result.ok(map);
    }

    @ApiOperation("订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyerId", value = "用户ID"),
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "显示条数")
    })
    @GetMapping("/list/{buyerId}/{page}/{size}")
    public Result list(@PathVariable("buyerId") Integer buyerId,@PathVariable("page") Integer page,@PathVariable("size")  Integer size) {
        Page<OrderMaster> orderMasterPage = new Page<>(page, size);
        LambdaQueryWrapper<OrderMaster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderMaster::getBuyerOpenid, buyerId);
        Page<OrderMaster> resultPage = orderMasterService.page(orderMasterPage);
        List<OrderMaster> records = resultPage.getRecords();
        return Result.ok(records);
    }

    @ApiOperation("查询订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyerId", value = "用户ID"),
            @ApiImplicitParam(name = "orderId", value = "订单ID")
    })
    @GetMapping("/detail/{buyerId}/{orderId}")
    public Result detail(@PathVariable("buyerId") Integer buyerId, @PathVariable("orderId") String orderId) {

        return Result.ok(orderMasterService.detail(buyerId, orderId));
    }

    @ApiOperation("取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyerId", value = "用户ID"),
            @ApiImplicitParam(name = "orderId", value = "订单ID")
    })
    @PutMapping("/cancel/{buyerId}/{orderId}")
    public Result cancel(@PathVariable("buyerId") Integer buyerId, @PathVariable("orderId") String orderId) {
        orderMasterService.cancel(buyerId, orderId);
        return Result.ok();
    }

    @ApiOperation("完结订单")
    @ApiImplicitParam(name = "orderId", value = "订单ID")
    @PutMapping("/finish/{orderId}")
    public Result finish(@PathVariable("orderId") String orderId) {
        orderMasterService.finish(orderId);
        return Result.ok();
    }

    @ApiOperation("支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyerId", value = "用户ID"),
            @ApiImplicitParam(name = "orderId", value = "订单ID")
    })
    @PutMapping("/pay/{buyerId}/{orderId}")
    public Result pay(@PathVariable("buyerId") Integer buyerId, @PathVariable("orderId") String orderId) {
        orderMasterService.pay(buyerId, orderId);
        return Result.ok();
    }


}

